package com.jwpdk.appframework.controller;

import java.net.URL;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;

import com.jwpdk.appframework.data.IDataList;
import com.jwpdk.common.Config;
import com.jwpdk.dbframework.SqlMap;
import com.jwpdk.util.StringUtils;

/**
 * 
 * @author 王志伟
 *
 */
public class Dispatcher {

	private final Logger		log			= Logger.getLogger(this.getClass());
	private static Dispatcher	dispatcher	= null;
	private static Config		config		= Config.getInstance();

	public static Dispatcher getInstance() {
		if ( dispatcher == null || config.getSysMode().equalsIgnoreCase(Config.SYSTEM_MODE_DEBUG) ) {
			dispatcher = new Dispatcher();
		}
		return dispatcher;
	}

	private Dispatcher() {}

	/**
	 * 所有配置
	 */
	private static Hashtable< String, String>	actions;

	/**
	 * 默认配置
	 */
	private ResourceBundle				rs_dispatcher;

	/**
	 * 初始化配置信息
	 */
	void initialization() throws Exception {
		// 加载默认配置
		actions = new Hashtable< String, String>();
		// 文件方式加载
		if(Config.ACTIONIMPTYPE_FILE.equals(config.getActionImpType())){
			getActionFromFile (config.getDispatcher());
		}else if(Config.ACTIONIMPTYPE_DB.equals(config.getActionImpType())){
			getActionFromDB(config.getDispatcher());
		}else {
			throw new Exception("Action加载配置信息错误！");
		}
		if(log.isDebugEnabled()){
			log.debug("Actions:" + actions);
		}
	}

	/**
	 * 重新加载
	 * @throws Exception
	 */
	public void reload() throws Exception{
		actions.clear();
		actions = null;
		rs_dispatcher = null;
		initialization();
	}
	
	/**
	 * 获取请求的Action全名。
	 * @param handlerCode
	 * @return
	 * @throws Exception
	 */
	public String getActionCode(String handlerCode) throws Exception{
		
		if(!actions.containsKey(handlerCode)&&Config.SYSTEM_MODE_DEBUG.equals(config.getSysMode())){
			reload();
		}
		if(!actions.containsKey(handlerCode)){
			throw new Exception(String.format("Action信息未配置%s",handlerCode));
		}
		return actions.get(handlerCode);
	}
	
	
	/**
	 * 从文件读取配置信息
	 * @param sqlref
	 * @throws Exception
	 */
	private void getActionFromFile(String fileString) throws Exception{
		URL url = Thread.currentThread().getContextClassLoader().getResource(fileString);
		if ( url != null ) {
			rs_dispatcher = ResourceBundle.getBundle(getResource(fileString));
			Enumeration enu = rs_dispatcher.getKeys();
			String key_name;
			if ( enu != null ) {
				while ( enu.hasMoreElements() ) {
					key_name = String.valueOf(enu.nextElement());
					actions.put(key_name,StringUtils.trim(rs_dispatcher.getString(key_name)));
				}
			}
		}else{
			throw new Exception("未找到Action配置信息！");
		}
	}
	/**
	 * 从数据库读取配置信息
	 * @param sqlref
	 * @throws Exception
	 */
	private void getActionFromDB(String sqlref) throws Exception{
		SqlMap sqlMap = new SqlMap();
		IDataList iDataList = sqlMap.iSelect(sqlref);
		for (int i = 0; i < iDataList.count(); i++) {
			actions.put(iDataList.get(i,"ACTION").toString(),iDataList.get(i,"CLASS").toString());
		}
	}
	
	/**
	 * 解析文件资源
	 * @param fileString
	 * @return
	 */
	private String getResource(String fileString){
		if(fileString.indexOf(".")>0){
			fileString = fileString.substring(0,fileString.lastIndexOf("."));
		}
		return fileString;
	}
	
}
